﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Services;
using System.Net;
using System.Configuration;



public partial class Front_Twitter : System.Web.UI.Page
{

   // oAuthExample.oAuthTwitter oAuth = new oAuthExample.oAuthTwitter();
    protected void Page_Load(object sender, EventArgs e)
    {
        //https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_flow&SESSION=cXJsLlhtXgOVk_MO7dGhBqOFS_LMpOGmB-OO3mrtNSLOpOnsMQDVFO2nxNq&dispatch=50a222a57771920b6a3d7b606239e4d529b525e0b7e69bf0224adecfb0124e9b61f737ba21b0819812f77a5508bed785ce95c4858b848445
        //http://twitter.com/direct_messages/new.xml?user=" + ranjana + "&text=" + test
       // http://www.stardeveloper.com/articles/display.html?article=2009062001&page=3
        //Status_Update();
        // http://dev.twitter.com/pages/oauth_single_token#csharp
        //Response.Write(tweetsAPI("ranjana.lanetteam@yahoo.com", "ranjanasharma", "YOUR TWEETS POST"));

        //oAuth.ConsumerKey = "jsYpMqFdGvELpJAOoOnU6w";
        //oAuth.ConsumerSecret = "APandhpo8HjrUeFxwEyqCrw40kA4y4jDB9FSEhLO7dI";
        //oAuth.Token = "148823057-3WltyfiLCehsLSR2YOxJJS4c3sU4V6A0dK43Za1S";
        //oAuth.TokenSecret = "zhPokH9rqKO9IWib3siwtSmXSWmB2xfVqfA2bU9Jx8";

        //string tweet = "";
        //// pick whichever one you want
        //tweet = "Droid Doesn’t — Things I Hate About My Verizon Droid | Shelly Palmer http://linktw.it/9vfyq0";
        //tweet = "The common name is Zhōngguó, which is 中國 in traditional Chinese or 中国 in simplified Chinese";
        //tweet = "Ranjana test";

        //string updateurl = "http://twitter.com/statuses/update.xml";
        //string strXmlResp = oAuth.oAuthWebRequest(oAuthExample.oAuthTwitter.Method.POST, updateurl, "status=" + tweet);

    }
    //public static void Status_Update1()
    //{
    //    oAuthExample.OAuthBase oAuth = new oAuthExample.OAuthBase();
    //    string consumerKey = "jsYpMqFdGvELpJAOoOnU6w";
    //    string consumerSecret = "APandhpo8HjrUeFxwEyqCrw40kA4y4jDB9FSEhLO7dI";
    //    string oauth_token = "148823057-3WltyfiLCehsLSR2YOxJJS4c3sU4V6A0dK43Za1S";
    //    string oauth_token_secret = "zhPokH9rqKO9IWib3siwtSmXSWmB2xfVqfA2bU9Jx8";
    //    Uri uri = new Uri("http://api.twitter.com/1/statuses/ update.xml");
    //    string normUri; string normParams;
    //    string timeStamp = oAuth.GenerateTimeStamp();
    //    string nonce = oAuth.GenerateNonce();
    //    System.Net.ServicePointManager.Expect100Continue = false;
    //    string oauth_signature = oAuth.GenerateSignature(uri, consumerKey, consumerSecret,
    //        oauth_token, oauth_token_secret, "POST", "", timeStamp, nonce, oAuthExample.OAuthBase.SignatureTypes.HMACSHA1.ToString(),
    //        out normUri, out normParams);
    //    WebClient wcl = new WebClient();
    //    wcl.Headers.Add("Authorization: OAuth " + "oauth_nonce=\"" + nonce + "\", " + "oauth_signature_method=\"HMAC-SHA1\", " + "oauth_timestamp=\"" + timeStamp + "\", " + "oauth_consumer_key=\"" + consumerKey + "\", " + "oauth_token=\"" + oauth_token + "\", " + "oauth_signature=\"" + oauth_signature + "\", " + "oauth_version=\"1.0\"");
    //    wcl.Encoding = System.Text.Encoding.UTF8; Console.WriteLine("Out:{0}?{1}", normUri, normParams);
    //    string reply = wcl.UploadString(normUri, "POST", normParams);
    //    Console.WriteLine(reply);
    //}
  //  public static void Status_Update()
  //  {
  //      OAuthBase oAuth = new OAuthBase();
       
  //      string consumerKey = "jsYpMqFdGvELpJAOoOnU6w";
  //      string consumerSecret = "APandhpo8HjrUeFxwEyqCrw40kA4y4jDB9FSEhLO7dI";
  //      string oauth_token = "148823057-3WltyfiLCehsLSR2YOxJJS4c3sU4V6A0dK43Za1S";
  //      string oauth_token_secret = "zhPokH9rqKO9IWib3siwtSmXSWmB2xfVqfA2bU9Jx8";
  //      Uri
  //uri = new Uri("http://api.twitter.com/1/statuses/update.xml");

  //      string normUri;
  //      string normParams;

  //      string timeStamp = oAuth.GenerateTimeStamp();
  //      string nonce = oAuth.GenerateNonce();
  //      string oauth_signature = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, oauth_token, oauth_token_secret, "POST", timeStamp,
  //nonce, oAuth.GenerateSignature. .HMACSHA1, out normUri, out normParams);

  //      WebClient wcl = new WebClient();
  //      wcl.Headers.Add("Authorization: OAuth " +
  //        "oauth_nonce=\"" + nonce + "\", " +
  //        "oauth_signature_method=\"HMAC-SHA1\", " +
  //        "oauth_timestamp=\"" + timeStamp + "\", " +
  //        "oauth_consumer_key=\"" + consumerKey + "\", " +
  //        "oauth_token=\"" + oauth_token + "\", " +
  //        "oauth_signature=\"" + oauth_signature + "\", " +
  //        "oauth_version=\"1.0\"");

  //      wcl.Encoding = System.Text.Encoding.UTF8;
  //      Console.WriteLine("Out:{0}?{1}", normUri,
  //normParams);
  //      string reply = wcl.UploadString(normUri, "POST", normParams);
  //      Console.WriteLine(reply);
  //  }
    //public void test()
    //{
    //    string url = "";
    //    string xml = "";
    //    oAuthExample.oAuthTwitter oAuth = new oAuthExample.oAuthTwitter();

    //    if (Request["oauth_token"] == null)
    //    {
    //        //Redirect the user to Twitter for authorization.
    //        //Using oauth_callback for local testing.
    //        oAuth.CallBackUrl = "http://localhost/";
    //        Response.Redirect(oAuth.AuthorizationLinkGet());
    //    }
    //    else
    //    {
    //        //Get the access token and secret.
    //        oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
    //        if (oAuth.TokenSecret.Length > 0)
    //        {
    //            //We now have the credentials, so make a call to the Twitter API.
    //            url = "http://twitter.com/account/verify_credentials.xml";
    //            xml = oAuth.oAuthWebRequest(oAuthExample.oAuthTwitter.Method.GET, url, String.Empty);
    //            // apiResponse.InnerHtml = Server.HtmlEncode(xml);

    //            //POST Test
    //            url = "http://twitter.com/statuses/update.xml";
    //            xml = oAuth.oAuthWebRequest(oAuthExample.oAuthTwitter.Method.POST, url, "status=" + oAuth.UrlEncode("Hello @swhitley - Testing the .NET oAuth API"));
    //            //  apiResponse.InnerHtml = Server.HtmlEncode(xml);
    //        }
    //    }
    //}
    //private String tweetsAPI(String sUsername, String sPassword, String sMessage)
    //{
    //    String strOutput = "";
    //    String sTwitterURL = "http://www.twitter.com/statuses/update.xml";
    //    if (sMessage.Length > 140)
    //        sMessage = sMessage.Substring(0, 140);
    //    sMessage = String.Format("&status={0}", sMessage);
    //    if (sUsername != null && sPassword != null && sMessage != null)
    //    {
    //        try
    //        {
    //            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sMessage);
    //            WebRequest wrRequest = WebRequest.Create(sTwitterURL);
    //            wrRequest.ContentType = "application/x-www-form-urlencoded";
    //            wrRequest.ContentLength = bytes.Length;
    //            wrRequest.Credentials = new NetworkCredential(sUsername, sPassword);
    //            wrRequest.Method = "POST";
    //            ServicePointManager.Expect100Continue = false;

    //            wrRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
    //            HttpWebResponse wrResponse = (HttpWebResponse)wrRequest.GetResponse();
    //            if (wrResponse.StatusCode.ToString() == "OK")
    //                strOutput = "Your message has been tweet";
    //            else
    //                strOutput = "Could not tweet your message to Twitter.";
    //        }
    //        catch (Exception e)
    //        {
    //            strOutput = e.ToString();
    //        }
    //    }
    //    else
    //    {
    //        strOutput = "Missing information, please check it.";
    //    }
    //    return strOutput;
    //}
    //protected void Button6_Click(object sender, EventArgs e)
    //{


    //    //Twitterizer.Framework.Twitter twit = new Twitterizer.Framework.Twitter("ranjisharma", "ranjanasharma");
    //    //twit.Status.Update("Test message from " + "twitterizer.  See the code in " + "tomorrow's post.");

    //    try
    //    {
    //        string msg, url;
    //        msg = "Test Tweeter";
    //        url = "www.zupons.com/default.aspx?dealid=19";
    //        //PostTweet();
    //        Tweet(msg, url);
    //    }
    //    catch (Exception ex)
    //    {

    //    }
    //}
    //public static void Tweet(string msg, string url)
    //{
    //    string twitterXMLResponse = String.Empty, tweet;
    //    int tweetLength = msg.Length + url.Length;

    //    if (tweetLength >= 140)
    //        tweet = msg.Substring(0, (140 - 3 - url.Length)) + "..." + url;
    //    else
    //        tweet = msg + url;

    //    try
    //    {
    //        // Tweet the LeaderBoard
    //        string twitterURL = "https://twitter.com/statuses/update.xml?status=" + HttpUtility.UrlEncode(tweet);
    //        HttpWebRequest request = WebRequest.Create(twitterURL) as HttpWebRequest;
    //        request.Credentials = new NetworkCredential("ranjana.lanetteam@yahoo.com", "ranjanasharma");
    //        request.PreAuthenticate = true;
    //        request.Method = "Post";
    //        request.ServicePoint.Expect100Continue = false;
    //        string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("zupons" + ":" + "ajeet2010"));
    //        request.Headers.Add("Authorization", "Basic " + user);
    //        request.ContentType = "application/x-www-form-urlencoded";
    //        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    //        {
    //            StreamReader reader = new StreamReader(response.GetResponseStream());

    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        if (!ex.Message.Equals("The remote server returned an error: (403) Forbidden."))
    //            throw new Exception(ex.Message);//"Error posting tweet.");
    //    }
    //}

    /*
 * A function to post an update to Twitter programmatically
 * Author: Danny Battison
 * Contact: gabehabe@hotmail.com
 */

    /// <summary>
    /// Post an update to a Twitter acount
    /// </summary>
    /// <param name="username">The username of the account</param>
    /// <param name="password">The password of the account</param>
    ///// <param name="tweet">The status to post</param>
    //public static void PostTweet()
    //{
    //    try
    //    {


    //        // encode the username/password
    //        string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("zupons" + ":" + "ajeet2010"));
    //        // determine what we want to upload as a status
    //        byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + "test");
    //        // connect with the update page
    //        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
    //        // set the method to POST
    //        request.Method = "POST";
    //        request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
    //        // set the authorisation levels
    //        request.Headers.Add("Authorization", "Basic " + user);
    //        request.ContentType = "application/x-www-form-urlencoded";
    //        // set the length of the content
    //        request.ContentLength = bytes.Length;

    //        // set up the stream
    //        Stream reqStream = request.GetRequestStream();

    //        // write to the stream
    //        reqStream.Write(bytes, 0, bytes.Length);
    //        // close the stream
    //        reqStream.Close();


    //        HttpWebResponse response = (HttpWebResponse)
    //        request.GetResponse();

    //        Stream responseStream = response.GetResponseStream();
    //        StreamReader returned = new StreamReader(responseStream);
    //    }
    //    catch (Exception ex) {/* DO NOTHING */}
    //}



}
